<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class block_process extends Admin_Controller
{
	function sortColumn()
	{
		global $CI;
		
		// model
		$CI->load->module_model('block.block_list_model');
		$col = $CI->uri->segment($CI->block_list_model->item_segment);
		
		$order = array(''=>'desc', 'desc'=>'asc', 'asc'=>'desc');
		foreach ($CI->block_list_model->columns as $k=>$v)
		{
			if (strcmp($k, $col) == 0)
			{
				$CI->block_list_model->cache->add($k, $order[$CI->block_list_model->cache->load($k)]);
			} else
				$CI->block_list_model->cache->add($k, '');
		}
		
		// go to original page
		redirect($CI->block_list_model->url_prefix.'/'.$CI->block_list_model->start);
	}
	
	function deleteAll()
	{
		global $CI;
		
		// model
		$CI->load->module_model('block.block_list_model');
		$flag = 0;
		if (isset($_POST['uid']) && is_array($_POST['uid']))
		{
			foreach ($_POST['uid'] as $id)
			{
				if ($CI->block_list_model->check_delete($id))
				{
					$flag = 1;
					$CI->db->delete(BLOCK, array(BLOCK_BLOCK_ID=>$id, BLOCK_FRANCHISE_ID=>$CI->block_list_model->franchise_id()));			
				}
			}
		}
		if ($flag)
			// message
			$CI->block_list_model->cache->add($CI->block_list_model->msg_cache_prefix, $CI->lang->line('list_delete_all'));
		else
			// message
			$CI->block_list_model->cache->add($CI->block_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete')." : ".$CI->block_list_model->error_string);
		
		// go to original page
		redirect($CI->block_list_model->url_prefix.'/'.$CI->block_list_model->start);
	}
	
	function deleteOne()
	{
		global $CI;
		
		// model
		$CI->load->module_model('block.block_list_model');
		
		$id = $CI->uri->segment($CI->block_list_model->item_segment);
		if ($CI->block_list_model->check_delete($id))
		{
			$CI->db->delete(BLOCK, array(BLOCK_BLOCK_ID=>$id, BLOCK_FRANCHISE_ID=>$CI->block_list_model->franchise_id()));
		
			// message
			$CI->block_list_model->cache->add($CI->block_list_model->msg_cache_prefix, $CI->lang->line('list_delete'));
		} else
			// message
			$CI->block_list_model->cache->add($CI->block_list_model->msg_cache_prefix, $CI->lang->line('list_cannot_delete')." : ".$CI->block_list_model->error_string);
		
		// go to original page
		redirect($CI->block_list_model->url_prefix.'/'.$CI->block_list_model->start);
	}	
	
	function saveItem()
	{
		global $CI;
		
		// model
		$CI->load->module_model('block.block_detail_model');
		
		// validate
		$CI->load->library('form_validation');
        $CI->form_validation->set_rules('name', 'Name', 'trim|required');
        if ($CI->form_validation->run() == FALSE)
		{
			// message
			$CI->block_detail_model->cache->add($CI->block_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->form_validation->error_string());
			$CI->block_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->block_detail_model->url_prefix.'/'.$CI->block_detail_model->start.'/'.$CI->block_detail_model->item);
		}
		
		// save?
		if (!$CI->block_detail_model->check())
		{
			// message
			$CI->block_detail_model->cache->add($CI->block_detail_model->msg_cache_prefix, $CI->lang->line('detail_cannot_save')." : ".$CI->lang->line('_name_duplicate'));
			$CI->block_detail_model->cache->add('post', $_POST);

			// go to original page
			redirect($CI->block_detail_model->url_prefix.'/'.$CI->block_detail_model->start.'/'.$CI->block_detail_model->item);
		}
		
		if ($CI->block_detail_model->hasBlock())
		{
			// update
			$sql = "UPDATE ".BLOCK." SET";
			$sql .= " ".BLOCK_NAME."=".$CI->db->escape($CI->input->post('name'));
			$sql .= ", ".BLOCK_CONTENT."=".$CI->db->escape($CI->input->post('content'));
			$sql .= ", ".BLOCK_BLOCK_TYPE."=".$CI->db->escape($CI->input->post('block_type'));
			$sql .= ", ".BLOCK_OBJ_ID."=".$CI->db->escape($CI->input->post('obj_id'));
			$sql .= " WHERE ".BLOCK_FRANCHISE_ID."=".$CI->block_detail_model->franchise_id();
			$sql .= " AND ".BLOCK_BLOCK_ID."=".$CI->block_detail_model->item;
			$CI->db->query($sql);
		} else {
			$CI->block_detail_model->item = $CI->block_detail_model->maxkey();

			// insert
			$data = array(
				BLOCK_NAME=>$CI->input->post('name'),
				BLOCK_CONTENT=>$CI->input->post('content'),
				BLOCK_BLOCK_TYPE=>$CI->input->post('block_type'),
				BLOCK_FUNCTION_NAME=>'get_'.strtolower($CI->input->post('block_type')).'_block',
				BLOCK_OBJ_ID=>$CI->input->post('obj_id'),
				BLOCK_FRANCHISE_ID=>$CI->block_detail_model->franchise_id(),
				BLOCK_BLOCK_ID=>$CI->block_detail_model->item,
			);
			$CI->db->insert(BLOCK, $data);
		}

		// message
		$CI->block_detail_model->cache->add($CI->block_detail_model->msg_cache_prefix, $CI->lang->line('detail_save'));
		
		// go to original page
		redirect($CI->block_detail_model->url_prefix2.'/'.$CI->block_detail_model->start);
	}		
}

?>